//
//  View+Change.swift
//  BasicTools
//
//  Created by S JZ on 2025/5/27.
//

import Foundation
import SwiftUI

extension BasicWrapper where Base: View {
    
    public func onChange<V>(of value: V, initial: Bool = false, _ action: @escaping (_ newValue: V) -> Void) -> some View where V : Equatable {
        if #available(iOS 17.0, *) {
            return base.onChange(of: value, initial: initial) { action($1) }
        }else {
            return base.onChange(of: value) { action($0) }
        }
    }
    
//    public func on<V>(of value: V, initial: Bool = false, _ action: @escaping () -> Void) -> some View where V : Equatable {
//        if #available(iOS 17.0, *) {
//            return base.onChange(of: value, initial: initial) { action() }
//        }else {
//            return base.onChange(of: value) { action() }
//        }
//    }
}

extension BasicWrapper where Base: Scene {
    public func onChange<V>(of value: V, initial: Bool = false, _ action: @escaping (_ newValue: V) -> Void) -> some Scene where V : Equatable {
        if #available(iOS 17.0, *) {
            return base.onChange(of: value, initial: initial) { action($1) }
        }else {
            return base.onChange(of: value) { action($0) }
        }
    }
    
//    public func on<V>(of value: V, initial: Bool = false, _ action: @escaping () -> Void) -> some Scene where V : Equatable {
//        if #available(iOS 17.0, *) {
//            return base.onChange(of: value, initial: initial) { action() }
//        }else {
//            return base.onChange(of: value) { action() }
//        }
//    }
}

